package com.kasuroid.test;

import com.kasuroid.R;
import com.kasuroid.core.Debug;
import com.kasuroid.test.cases.GameStates.TestCaseGameStateActivity;
import com.kasuroid.test.cases.Timers.TestCaseTimersActivity;
import com.kasuroid.test.cases.Textures.TestCaseStarsActivity;
import com.kasuroid.test.cases.Textures.TestCaseStarsAndrActivity;
import com.kasuroid.test.cases.Textures.TestCaseCirclesActivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class KasuroidTestActivity extends Activity 
{
    /** Holder of the test groups list. */
    private ListView mList;

    private boolean mGroupListVisible = false;

    private boolean mTestListVisible = false;

    private TestManager mTestManager; 
    
    private int mCurrentGroup = -1;
    
    private int mCurrentTest = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        mTestManager = new TestManager(this);

        setContentView(R.layout.kasuroid_test_layout);
        mList = (ListView)findViewById(R.id.id_kasuroid_test);

        // Initialize the tests here.
        TestCaseGroup group = new TestCaseGroup("Game states");
        group.addTestCase(new TestCase("State 1", TestCaseGameStateActivity.class));
        mTestManager.addTestGroup(group);

        group = new TestCaseGroup("Timers");
        group.addTestCase(new TestCase("Two timers", TestCaseTimersActivity.class));
        mTestManager.addTestGroup(group);

        group = new TestCaseGroup("Textures");
        group.addTestCase(new TestCase("Stars", TestCaseStarsActivity.class));
        group.addTestCase(new TestCase("Andro Stars", TestCaseStarsAndrActivity.class));
        group.addTestCase(new TestCase("Circles", TestCaseCirclesActivity.class));
        mTestManager.addTestGroup(group);
        showTestGroupList();

        mList.setOnItemClickListener(new OnItemClickListener() 
        {
            @Override
            public void onItemClick(AdapterView<?> a, View v, int position, long id)
            {
                if (mGroupListVisible == true)
                {
                    Debug.inf(getClass().getName(), "position: " + position);

                    if (position >= 0)
                    {
                        mCurrentGroup = position;
                        // Position is a group id.
                        showTestList(mCurrentGroup);
                    }
                }
                else if (mTestListVisible == true)
                {
                    Debug.inf(getClass().getName(), "position: " + position);

                    if (position >= 0)
                    {
                        // Position is a test id.
                        Debug.inf(getClass().getName(), "Running test with id = " + position);
                        //mTestManager.run(position);
                        mCurrentTest = position;
                        mTestManager.run(mCurrentGroup, mCurrentTest);
                    }
                }
            }
        });
    }

    @Override
    protected void onDestroy() 
    {
        super.onDestroy();
    }
    
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEYCODE_BACK) 
        {
            if (mTestListVisible == true)
            {
                // Test list is visible so go back to the group
                // list.
                showTestGroupList();
                return true;
            }
            else if (mGroupListVisible == true)
            {
                // Group list visible so can't go back. Exit the activity.
                Debug.inf(getClass().getName(), "Exiting the activity");
                return super.onKeyDown(keyCode, event);
            }
        }

        return super.onKeyDown(keyCode, event);
    }

    private void showTestGroupList()
    {
        String[] groups = mTestManager.getGroupStringList();
        if (groups.length > 0)
        {
            Debug.inf(getClass().getName(), "Switched to the group list view!");
            mList.setAdapter(new ArrayAdapter<String>(this, 
                    android.R.layout.simple_list_item_1, 
                    mTestManager.getGroupStringList()));
            mGroupListVisible = true;
            mTestListVisible = false;
            mCurrentGroup = -1;
            mCurrentTest = -1;
        }
        else
        {
            Debug.warn(getClass().getName(), "No groups available!");
        }
    }

    private void showTestList(int groupId)
    {
        String[] tests = mTestManager.getGroupTestsStringList(groupId);
        if (tests.length > 0)
        {
            Debug.inf(getClass().getName(), "Switched to the test list view!");
            mList.setAdapter(new ArrayAdapter<String>(this, 
                    android.R.layout.simple_list_item_1,
                    mTestManager.getGroupTestsStringList(groupId)));
            mGroupListVisible = false;
            mTestListVisible = true;
            mCurrentTest = -1;
        }
        else
        {
            Debug.warn(getClass().getName(), "No tests available in group with id = " + groupId);
        }
    }
}
